﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ROMAP.Utilities;
using Microsoft.Xna.Framework;

namespace ROMAP.Entities.Tiles
{
    public class TileGrid : Entity
    {
        #region Fields

        private Dictionary<Vector2i, Tile> _grid;

        #endregion

        #region Constructors

        public TileGrid()
        {
            Initialize();
        }

        #endregion

        #region Initialization
        protected override void Initialize()
        {
            base.Initialize();

            _grid = new Dictionary<Vector2i, Tile>();
        }
        #endregion

        #region Activity
        public override void Activity()
        {

        }

        #endregion

        #region Destroy

        public void Destroy()
        {
            base.Destroy();

            foreach (Tile t in _grid.Values)
            {
                t.Destroy();
            }

            _grid.Clear();
        }

        #endregion

        #region Dictionary Manipulation

        public void Add(Tile t)
        {
            t.AttachTo(this, false);
            if (!_grid.ContainsKey(t.TilePosition))
            {
                    _grid.Add(t.TilePosition, t);
            }
        }

        public void Remove(Tile t)
        {
            _grid.Remove(t.TilePosition);

            t.Destroy();
        }

        public void Remove(Vector2i pos)
        {
            Tile t;
            _grid.TryGetValue(pos, out t);

            _grid.Remove(t.TilePosition);

            t.Destroy();
        }

        public Tile Get(Vector2i pos)
        {
            Tile t;

            _grid.TryGetValue(pos, out t);

            return t;
        }

        public List<Tile> GetAdjacentTiles(Vector2i tilePos)
        {
            List<Tile> tiles = new List<Tile>();

            for (int i = -1; i < 2; i++)
            {
                for (int j = -1; j < 2; j++)
                {
                    Tile temp = Get(new Vector2i(tilePos.X + i, tilePos.Y + j));

                    if (temp != null)
                    {
                        tiles.Add(temp);
                    }
                }
            }

            return tiles;
        }

        public List<Tile> GetAdjacentTiles(Entity e)
        {
            List<Tile> tiles = new List<Tile>();

            Vector2i pos = e.TilePosition;

            for (int i = -1; i < 2; i++)
            {
                for (int j = -1; j < 2; j++)
                {
                    Tile temp = Get(new Vector2i(pos.X + i, pos.Y + j));

                    if (temp != null)
                    {
                        tiles.Add(temp);
                    }
                }
            }

            return tiles;
        }

        public List<Tile> GetPulseTiles(Entity e)
        {
            List<Tile> tiles = new List<Tile>();

            Vector2i pos = e.TilePosition;

            for (int i = -2; i < 3; i++)
            {
                for (int j = -2; j < 3; j++)
                {
                    Tile temp = Get(new Vector2i(pos.X + i, pos.Y + j));

                    if (temp != null)
                    {
                        tiles.Add(temp);
                    }
                }
            }

            return tiles;
        }

        public bool Contains(Vector2i pos, Type type)
        {
            Tile temp = Get(new Vector2i(pos.X, pos.Y));

            if (temp != null && type.Equals(temp.GetType()))
            {
                return true;
            }
            else
                return false;
        }

        #endregion

    }
}
